<template>
  <div class="resource-list">
    <el-card class="box-card">
      <el-button size="small" @click="handleEdit">添加</el-button>
       <el-divider />
      <el-table
        :data="resourceCategories"
        size="medium"
        style="width: 100%; margin-bottom: 20px"
        v-loading="isLoading"
      >
        <el-table-column
          type="index"
          label="编号"
          width="100">
        </el-table-column>
        <el-table-column
          prop="name"
          label="名称"
          width="180">
        </el-table-column>
        <el-table-column
          prop="createdTime"
          label="创建时间">
        </el-table-column>
        <el-table-column
          prop="sort"
          label="排序">
        </el-table-column>
        <el-table-column
          width="180"
          label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleEdit(scope.row)">编辑</el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 编辑新建弹窗 -->
    <create-or-edit-class v-if="dialogVisible" :dialogVisible.sync="dialogVisible" :isEdit="isEdit" @reloadList="loadResourceCategories" :formData="formData" />
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
import { getResourcePages } from '@/services/resource'
import { getResourceCategories, delResourceCategories } from '@/services/resource-category'
import { Form } from 'element-ui'
import CreateOrEditClass from './CreateOrEditClass.vue'

export default Vue.extend({
  name: 'ClassList',
  components: {
    CreateOrEditClass
  },
  data () {
    return {
      resources: [], // 资源列表
      dialogVisible: false, // 弹窗状态
      totalCount: 0,
      formData: {},
      form: {},
      isEdit: false,
      resourceCategories: [], // 资源分类列表
      isLoading: true // 加载状态
    }
  },

  created () {
    this.loadResourceCategories()
  },

  methods: {
    async loadResourceCategories () {
      this.isLoading = true
      const { data } = await getResourceCategories()
      this.isLoading = false
      this.resourceCategories = data.data
    },

    handleEdit (item: any) {
      this.isEdit = false
      if (item.id) {
        this.isEdit = true
      }
      this.formData = item.id ? item : {}
      this.dialogVisible = true
    },

    async handleDelete (item: any) {
      this.$confirm('确认删除吗？', '删除提示', {})
        .then(async () => { // 确认执行这里
          // 请求删除操作
          const { data } = await delResourceCategories(item.id)
          if (data.code === '000000') {
            this.$message.success('删除成功')
            this.loadResourceCategories() // 更新数据列表
          }
        })
        .catch(err => { // 取消执行这里
          console.log(err)
          this.$message.info('已取消删除')
        })
    }

  }
})
</script>

<style lang="scss" scoped></style>
